package com.zzxz.practicaltrainingsys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzxz.practicaltrainingsys.vo.ViewStudent;
import com.zzxz.practicaltrainingsys.mapper.StudentMapper;
import com.zzxz.practicaltrainingsys.entity.Student;
import com.zzxz.practicaltrainingsys.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public List<Student> getStudent(){
        return studentMapper.getStudent();
    }

    @Override
    public List<String> getStudentNames() {
        return studentMapper.getStudentNames();
    }

    @Override
    public PageInfo<Student> getStudentPages(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Student> list = studentMapper.getStudent();
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<ViewStudent> findStudentPages(Integer pageNum, Integer pageSize, Integer school_id, Integer college_id, Integer major_id, Integer grade_id) {
        PageHelper.startPage(pageNum, pageSize);
        List<ViewStudent> list = studentMapper.findStudentByCondition(school_id, college_id, major_id, grade_id);
        return new PageInfo<>(list);
    }

    @Override
    @Transactional
    public boolean updatePassword(Integer student_id, String student_password) {
        // 参数校验
        if (student_id == null || student_password == null || student_password.isEmpty()) {
            throw new IllegalArgumentException("学号和密码不能为空");
        }

        // 调用Mapper更新密码
        int affectedRows = studentMapper.updatePassword(student_id, student_password);

        // 返回是否更新成功
        return affectedRows > 0;
    }

    @Override
    public Student getStudentById(Integer student_id) {
        return studentMapper.getStudentById(student_id);
    }

    @Override
    public boolean updateStudent(Student student) {
        return studentMapper.updateStudent(student) > 0;
    }

    @Override
    public boolean addStudent(Student student) {
        return studentMapper.addStudent(student) > 0;
    }

    @Override
    public ViewStudent getViewStudentById(Integer student_id) {
        return studentMapper.findViewStudentById(student_id);
    }


    @Override
    public boolean logicDeleteStudent(Integer student_id) {
        return studentMapper.logicDeleteStudent(student_id) > 0;
    }
    @Override
    public boolean logicDeleteStudents(List<Integer> ids) {
        return studentMapper.logicDeleteStudents(ids) > 0;
    }

    @Override
    public int getTotalStudentsNum() {
        return studentMapper.getTotalStudentsNum();
    }

    @Override
    public Student findStudentByPhone(String student_phone) {
        return studentMapper.findStudentByPhone(student_phone);
    }



    @Override
    public List<Student> getStudentsByProjectId(Integer projectId) {
        return studentMapper.getStudentsByProjectId(projectId);

    }


}